<?php
/**
 * Modified from @gordon's answer: http://stackoverflow.com/questions/4398951/force-ssl-https-using-htaccess-and-mod-rewrite
 * This code forces this page to SSL, but only if not called from the localhost URL
 */
function GetURL()
{

if (!strstr(url,'http://localhost'))
{
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
    if(!headers_sent()) {
        header("Status: 301 Moved Permanently");
        header(sprintf(
            'Location: https://%s%s',
            $_SERVER['HTTP_HOST'],
            $_SERVER['REQUEST_URI']
        ));
        exit();
    }
}
}
}
?>
<html>
<head>
<title>Log Viewer</title>
</head>
<body>
    <form action="LogViewer.php" method="post">
        <b>Password</b><input type="text" name="password" value="" /> <br /> 
        ---------------------------------<br>
        <b>Method 1: </b>Select the log level you'd like to see. Levels run from 
            1-99, where 1 is the most severe and 99 is routine messages. Select 
            either &lt; or "=" and the log level to filter on.<br>
        View entries with a log level <input type="text" name="lessOrEqual" value="<" size="1"/> 
        level <input type="text" name="level" value="30" size="1"/>. <br />
        (Optional) Filter on method/source name: <input type="text" name="method" value="" size="50"/> <br/>
        <input type="submit" name="submit" /><br/>
        <b><i>BROKEN, USE METHOD 1</i> Method 2:</b>Display the last n entries from the log.<br>
        Get last <input type="text" name="entrycount" value="20" size="3"/> entries: <input type="submit" name="submitLastN"/><br/>
    </form>
<?php
require_once 'Database.php';
class LogViewer
{
    //TODO This is insecure -- password embedded in code. 
    //static $password = "";
    public static function GetLogEvents ()
    {
        if ($_POST["lessOrEqual"] === "=") {
            $lessOrEqual = "=";
        } else {
            $lessOrEqual = "<";
        }
        if (is_numeric($_POST["level"])) {
            $level = $_POST["level"];
        } else {
            $level = 30;
        }
        if (strlen($_POST["method"]) > 0) {
            $method = $_POST["method"];
        } else {
            $method = "";
        }
        $db = new Database();
        $db->connect();
        $rows = $db->getLogEntries($lessOrEqual, $level, $method);
        return $rows;
    }
    
    public static function GetPassword()
    {
    	$db = new Database();
    	$db->connect();
    	return $db->getConfiguration("LogViewerPassword");
    }
    
    public static function GetLastNEvents()
    {
        $db = new Database();
        $db->connect();
        $n = $_POST["entrycount"];
        $rows = $db->getLastNEntries($n);
        return $rows;
    }
    public static function WriteRow ($row)
    {
        $rowStartTag = '<tr>';
        $rowEndtag = '</tr>';
        $cellStartTag = '<td>';
        $cellEndTag = '</td>';
        echo $rowStartTag;
        echo $cellStartTag . $row['level'] . $cellEndTag;
        echo $cellStartTag . substr($row['timeOfEvent'], 5) . $cellEndTag;
        echo $cellStartTag . $row['source'] . $cellEndTag;
        echo $cellStartTag . $row['description'] . $cellEndTag;
        echo $rowEndtag;
    }
    public static function WriteRows ($rows)
    {
        echo 'length: ' . count($rows);
        for ($counter = 0; $counter < count($rows); $counter ++) {
            LogViewer::WriteRow($rows[$counter]);
        }
    }
}
/**
 * The stored procedure for the "get last n entries" option works on a windows 
 * machine, but not on an amazon linux instance. I cannot for the life of me 
 * figure out why, but I know it is the stored procedure because I cannot get 
 * mysql to take it when I pipe in airidentity.sql from the command line. TODO
 */
if (isset($_POST['submitLastN'])) {
    if ($_POST["password"] == LogViewer::GetPassword()) {
        $n = $_POST["entrycount"];
        echo "<h1>Returning last $n entries:</h1>";
        echo '<table>';
        echo '<th></th>';
        $rows = LogViewer::GetLastNEvents();
        LogViewer::WriteRows($rows);
    }
} elseif (isset($_POST['submit'])) {
    echo $_POST["lessOrEqual"] . "<br/>";
    echo $_POST["level"] . "<br/>";
    echo $_POST["method"] . "<br/>";
    echo '<table>';
    echo '<th></th>';
    if ($_POST["password"] == LogViewer::GetPassword()) {
        $rows = LogViewer::GetLogEvents();
        LogViewer::WriteRows($rows);
    }
}
?>
</body>
</html>